package com.ourhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ourhealth.mapper.MenuMapper;
import com.ourhealth.mapper.RoleMapper;
import com.ourhealth.pojo.Menu;
import com.ourhealth.pojo.Role;
import com.ourhealth.pojo.User;
import com.ourhealth.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author ych
 * @version v1.0
 * @date 2019/12/3 16:09
 * @description
 **/

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<Menu> findMenus(String username) {
        List<Menu> list = new ArrayList<>();
//        根据用户id查询角色
        User user = menuMapper.findUserByUserName(username);
        Integer userId = user.getId();
        Set<Role> roles = roleMapper.findRolesByUserId(userId);
//        遍历roles
        for (Role role : roles) {
            Integer roleId = role.getId();
            List<Integer> menuIds = menuMapper.findMenuIdsByRoleId(roleId);
            for (Integer menuId : menuIds) {
//                根据菜单id查询出所有系统菜单
                Menu menu = menuMapper.findMenuByMenuId(menuId);
                list.add(menu);
            }
        }
//           查询一级菜单
        List<Menu> firstMenus = new ArrayList<>();
        for (Menu menu : list) {
            Integer parentMenuId = menu.getParentMenuId();
            if (null == parentMenuId) {
                firstMenus.add(menu);
            }
        }
//            查询设置子菜单
        for (Menu firstMenu : firstMenus) {
            Integer menuFirstId = firstMenu.getId();
            List<Menu> secondMenus = new ArrayList<>();
//            遍历所有菜单
            for (Menu menu : list) {
                Integer parentMenuId = menu.getParentMenuId();
//                如果父菜单id为null则结束当前循环
                if (parentMenuId == null) {
                    continue;
                }
//            如果父菜单id等于当前id则加入集合
                if (menuFirstId.equals(parentMenuId)) {
                    secondMenus.add(menu);
                    firstMenu.setChildren(secondMenus);
                }
            }
            List<Menu> thirdMenu = new ArrayList<>();
//            遍历所有二级菜单
            for (Menu secondMenu : secondMenus) {
                Integer secondId = secondMenu.getId();
                for (Menu menu : list) {
                    Integer parentMenuId = menu.getParentMenuId();
                    if (parentMenuId == null) {
                        continue;
                    }
                    if (secondId.equals(parentMenuId)) {
                        thirdMenu.add(menu);
                        secondMenu.setChildren(thirdMenu);
                    }
                }
            }
        }

        return firstMenus;
    }
}